﻿#include "TCPClientDlg.h"
#include "Note.h"
#include "Application.h"

TCPClientDlg::TCPClientDlg(QWidget *parent)
	: BaseDlg(parent,"TCP客户端通道")
{
	ui.setupUi(&content_view);
	this->base_ui.content->addWidget(&content_view);
	//connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(slotApply()));
	connect(ui.checkBox, &QCheckBox::toggled, this, &TCPClientDlg::togglog_sync);
	connect(ui.comboBox_RecvDataName, &QComboBox::currentIndexChanged, this, &TCPClientDlg::RecvDataChanged);


	ui.comboBox_RecvDataName->clear();
	ui.comboBox_SendDataName->clear();
	for(auto& item : Application::Instance().cfg.DataTypes())
	{
		ui.comboBox_RecvDataName->addItem(item.second.c_str(), item.first);
		ui.comboBox_SendDataName->addItem(item.second.c_str(), item.first);
	}
}

TCPClientDlg::~TCPClientDlg()
{
}



bool TCPClientDlg::get_info(ChannelCreateInfo& info)
{
	info.type |= CHANNEL_PROTO_TYPE_TCP_CLIENT;
	auto title = ui.lineEdit_ChannName->text();
	if(title.isEmpty())
	{
		Note nt{ "通道名称不能为空！" };
		nt.showDialog();
		return false;
	}
	strcpy(info.title, ui.lineEdit_ChannName->text().toStdString().c_str());

	auto local_port = ui.spinBox_LocalPort->value();
	if(local_port != 0)
	{
		info.input_address.init_client(static_cast<USHORT>(local_port));
	}

	struct
	{
		bool is_ok;
		unsigned int key;
	}data_pair[2]{};

	data_pair[0].key = ui.comboBox_RecvDataName->currentData().toUInt(&data_pair[0].is_ok);
	data_pair[1].key = ui.comboBox_RecvDataName->currentData().toUInt(&data_pair[1].is_ok);

	if(!(data_pair[0].is_ok && data_pair[1].is_ok))
	{
		Note nt{ "收发数据类型异常！" };
		nt.showDialog();
		return false;
	}

	info.input_type = data_pair[0].key;
	info.output_type = data_pair[1].key;

	if(!info.output_address.init(ui.lineEdit_PeerIP->text().toStdString().c_str(), ui.lineEdit_PeerPort->value()))
	{
		Note nt{ "服务端IP不正确" };
		nt.showDialog();
		return false;
	}
	return true;
}

void TCPClientDlg::RecvDataChanged(int index)
{
	if(ui.checkBox->isChecked())
	{
		ui.comboBox_SendDataName->setCurrentIndex(index);
	}
}

void TCPClientDlg::togglog_sync(bool b)
{
	ui.comboBox_SendDataName->setEnabled(!b);
	if(b)
	{
		ui.comboBox_SendDataName->setCurrentIndex(ui.comboBox_RecvDataName->currentIndex());
	}
}
//
//void TCPClientDlg::slotCheck()
//{
//	if (ui.checkBox->isChecked())
//	{
//		ui.comboBox_SendDataName->setEnabled(false);
//		ui.comboBox_SendDataName->setCurrentIndex(ui.comboBox_RecvDataName->currentIndex());
//	}
//	else
//		ui.comboBox_SendDataName->setEnabled(true);
//}
//
//void TCPClientDlg::slotApply()
//{
//	ConnInfo info;
//
//	info.type = ConnType_Client_TCP;
//	info.chanName = ui.lineEdit_ChannName->text();
//	info.localIP = ui.comboBox_LocalIP->currentText();
//	info.localPort = ui.spinBox_LocalPort->text().toInt();
//	info.peerIP = ui.lineEdit_PeerIP->text();
//	info.peerPort = ui.lineEdit_PeerPort->text().toInt();
//	info.recvDataName = ui.comboBox_RecvDataName->currentText();
//	info.sendDataName = ui.comboBox_SendDataName->currentText();
//
//	std::vector<UDPSendCgf> lstUDPSendCgf;
//	if ((m_stConnInfo.peerIP != info.peerIP || m_stConnInfo.peerPort != info.peerPort)
//		&& !m_bNewConnInfo)
//	{
//		emit sigDisConnect(m_stConnInfo, info);
//	}
//
//	emit sigConnInfo(info, m_stConnInfo, m_bNewConnInfo);
//	m_bNewConnInfo = false;
//	m_stConnInfo = info;
//
//	this->hide();
//}
